package org.mch.func.file.copyfiletofolder;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;

import org.apache.commons.io.FileUtils;
import org.mch.biz.ui.FuncEvent;
import org.mch.biz.ui.FuncPanel;
import org.mch.swingm.core.MFileChooser;

/**
 * 描述：文件操作；
 * 
 * @author MCH；Date：2008-10-01；<BR/>
 * 
 */
public class ClientEvent extends FuncEvent {
	private static final long serialVersionUID = -8590108060646853821L;

	MFileChooser fileChooser;

	public ClientEvent(FuncPanel panel) {
		super(panel);
	}

	protected void onButtonClick(ActionEvent e) throws Exception {
		if (e.getSource() == getPanel().getBtn_execute()) {
			execute();
		}
	}

	public void execute() {
		String src_file = getPanel().getText_copy_srcfile().getValue();
		String parent_folder = getPanel().getText_copy_parantfolder().getValue();

		File srcfile = new File(src_file);

		File parentfolder = new File(parent_folder);
		File[] files = parentfolder.listFiles();

		for (File file : files) {
			if (file.isDirectory() && !file.isHidden()) {
				try {
					FileUtils.copyFile(srcfile, new File(file.getPath() + File.separator + srcfile.getName()));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public MFileChooser getFileChooser() {
		if (fileChooser == null) {
			fileChooser = new MFileChooser();
			fileChooser.setPreferredSize(700, 400);
			fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);// 设置能选择文件和文件夹
		}
		return fileChooser;
	}

	public ClientPanel getPanel() {
		return (ClientPanel) super.getPanel();
	}
}